import pandas as pd
import json
import os
import gzip

def loan_data(directory):
    # 创建一个空的 DataFrame
    df = pd.DataFrame()

    # 遍历文件
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)

            # 剔除其他格式文件
            if not file.endswith(".gz"):
                continue

            # 读取 JSON
            # data = json.load(open(file_path, 'r'))
            data = json.load(gzip.open(file_path, 'rb'))

            # 将 JSON 数据转换为 DataFrame
            temp_df = pd.DataFrame(data)
            # 增加 id 列
            temp_df.insert(loc=0, column='id', value=file.split(".")[0])
            # DataFrame拼接
            df = df.append(temp_df, ignore_index=True)

        # 将 DataFrame 写入 Excel 文件
        df.to_excel('output.xlsx')


if __name__ == "__main__":
    loan_data('/Users/wangjianwen/Downloads/addressbook1')